#ifndef __COMPOSITE_H__
#define __COMPOSITE_H__

#include <iostream>
#include <vector>
#include <algorithm>

/// @brief 公司接口
class CompanyInterface
{
protected:
    std::string m_name{};

public:
    std::string GetName() const
    {
        return m_name;
    }
    CompanyInterface(const std::string &name)
        : m_name(name)
    {
    }
    /// @brief 添加节点
    /// @param com 节点指针
    virtual void Add(CompanyInterface *com) {}
    /// @brief 移除节点
    /// @param com 节点指针
    virtual void Remote(CompanyInterface *com) {}
    /// @brief 显示公司组织结构
    /// @param depth 当前深度，打印用
    virtual void ShowCompanyOrganic(int depth) = 0;
    /// @brief 部门人物处理
    virtual void Process() = 0;
};

/// @brief 公司实体
class Company : public CompanyInterface
{
private:
    std::vector<CompanyInterface *> m_sub; // 保存子部门和子公司

public:
    Company(const std::string &name)
        : CompanyInterface(name)
    {
    }
    virtual void Add(CompanyInterface *com) override
    {
        m_sub.push_back(com);
    }
    virtual void Remote(CompanyInterface *com) override
    {
        auto it = std::find(m_sub.begin(), m_sub.end(), com);
        CompanyInterface *pCom = *it;
        m_sub.erase(it);
        printf("移除 %s\n", pCom->GetName().c_str());
    }
    virtual void ShowCompanyOrganic(int depth) override
    {
        std::string strDep(depth, '-');
        printf("%s%s\n", strDep.c_str(), m_name.c_str());
        for (auto it : m_sub)
        {
            it->ShowCompanyOrganic(depth + 1);
        }
    }
    virtual void Process() override
    {
        for (auto it : m_sub)
        {
            it->Process();
        }
    }
};
/// @brief 财政部
class Treasury : public CompanyInterface
{
public:
    Treasury(const std::string &name)
        : CompanyInterface(name)
    {
    }
    virtual void ShowCompanyOrganic(int depth) override
    {
        std::string strDep(depth, '-');
        printf("%s%s\n", strDep.c_str(), m_name.c_str());
    }
    virtual void Process() override
    {
        printf("%s 处理财政业务\n", m_name.c_str());
    }
};
/// @brief 人力资源部
class HumanResource : public CompanyInterface
{
public:
    HumanResource(const std::string &name)
        : CompanyInterface(name)
    {
    }
    virtual void ShowCompanyOrganic(int depth) override
    {
        std::string strDep(depth, '-');
        printf("%s%s\n", strDep.c_str(), m_name.c_str());
    }
    virtual void Process() override
    {
        printf("%s 处理人力资源业务\n", m_name.c_str());
    }
};

#endif